﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using WebApp.Data;
using WebApp.Commons;
using System.Transactions;

namespace WebApp.Services
{
    public class ProjectService
    {
        public List<tbl_project> GetAreaType(int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string keyword,string keyword2,string keyword3,string keyword4,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";
                
                //count = db.tbl_customers.Count(u => u.customer_firstname.Equals(keyword));

                var projs = db.tbl_projects.Any(p => ((string.IsNullOrEmpty(keyword)) || (p.tbl_customer.customer_firstname.Contains(keyword)))
                                                     & ((string.IsNullOrEmpty(keyword2)) || (p.tbl_customer.customer_tel.Contains(keyword2)))
                                                     & ((string.IsNullOrEmpty(keyword3)) || (p.project_location.Contains(keyword3)))
                                                     & ((string.IsNullOrEmpty(keyword4)) || (p.project_create_date.Equals(keyword4))));

                //return db.tbl_projects.Where(u => u.tbl_customer.customer_firstname.Contains(keyword)).ToList();
                return db.tbl_projects.Where(u => ((string.IsNullOrEmpty(keyword)) || (u.tbl_customer.customer_firstname.Contains(keyword)))
                                                     & ((string.IsNullOrEmpty(keyword2)) || (u.tbl_customer.customer_tel.Contains(keyword2)))
                                                     & ((string.IsNullOrEmpty(keyword3)) || (u.project_location.Contains(keyword3)))
                                                     & ((string.IsNullOrEmpty(keyword4)) || (u.project_create_date.Equals(keyword4)))).ToList();
            }

        }

        public List<tbl_project> GetProjectListByCustId(int index,
                                       int size,
                                       string sortColumn,
                                       string sortDirection,
                                       string custId,
                                       ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortDirection.Equals("desc") ? " descending " : "";

                //count = db.tbl_customers.Count(u => u.customer_firstname.Equals(keyword));

                return db.tbl_projects.Where(p => ((!string.IsNullOrEmpty(custId)) && (p.customer_id.ToString().Equals(custId)))).ToList();

            }
        }

        public tbl_customer GetCustName(int index)
        {
            using (var db = new PAVINGDataContext())
            {

                //count = db.tbl_customers.Count(u => u.customer_firstname.Equals(keyword));

                //return db.tbl_projects.Where(u => u.tbl_customer.customer_firstname.Contains(keyword)).ToList();
                return db.tbl_customers.SingleOrDefault(u => u.customer_id.Equals(index));
            }

        }

        public bool Update(tbl_project model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {

                    tbl_project eModel;

                    if (model.project_id == 0)
                    {
                        model.project_create_by = PavingContext.CurrentUserId();
                        model.project_create_date = DateTime.Now;
                        model.customer_id = model.customer_id;

                        db.tbl_projects.InsertOnSubmit(model);
                    }
                    else
                    {
                        eModel = db.tbl_projects.SingleOrDefault(s => s.project_id.Equals(model.project_id));

                        if (eModel != null)
                        {
                            eModel.project_location = model.project_location;

                            eModel.project_update_by = PavingContext.CurrentUserId(); ;
                            eModel.project_update_date = DateTime.Now;
                        }
                    }

                    db.SubmitChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int[] ids)
        {
            using (var tran = new TransactionScope())
            {
                try
                {
                    using (var db = new PAVINGDataContext())
                    {

                        foreach (var id in ids)
                        {
                            var eModel = db.tbl_projects.SingleOrDefault(s => s.project_id.Equals(id));

                            if (eModel == null) continue;
                            db.tbl_projects.DeleteOnSubmit(eModel);
                            db.SubmitChanges();
                        }

                        tran.Complete();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

    }
}